﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NWebCrawlerLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WF_DataAccess;
using NWebCrawlerLib;
using WF_Business;
using Common;

namespace Picdownload
{
    public partial class Form1 : Form
    {
        string strSql = "";
        string id = "";
        private string destDir;//目标文件夹
        public List<string> zblx = new List<string>();//招标类型
        public string baseUrl = "";
        public string fxUrl = "";
        public int k = 0;
        public Form1()
        {
            InitializeComponent();

        }

        private void btnSelectDestDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtDestDir.Text = dlg.SelectedPath;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            id = Guid.NewGuid().ToString();
            baseUrl = ConfigurationManager.AppSettings["baseUrl"];
            fxUrl=ConfigurationManager.AppSettings["fxurl"];
            string[] newurl = new string[1000];
            string keyword = txtKeyWords.Text;
            if (string.IsNullOrEmpty(keyword))
            {
                MessageBox.Show("请输入关键词");
                return;
            }
            destDir = txtDestDir.Text;
            if (string.IsNullOrEmpty(destDir))
            {
                MessageBox.Show("请选择要保存的文件夹");
                return;
            }

            for (int i = 0; i < chkZblx.CheckedItems.Count; i++)
            {
                zblx.Add(chkZblx.CheckedItems[i].ToString());
            }

            //默认d:\a\
            if (!destDir.EndsWith("\\"))
            {
                destDir = destDir + "\\";
            }
            //AutoResetEvent[] autoEvents = new AutoResetEvent[1];
            //autoEvents[0] = new AutoResetEvent(false);
            //避免了阻塞主线程
            Thread thread = new Thread(() => {
                ProcessDownload(keyword, fxUrl);
            });
            thread.Start();//如鹏网的《不拖控件的.Net》
            //WaitHandle.WaitAny(autoEvents);
        }

        private void ProcessDownload(string keywords, string fxUrl1)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fxUrl1);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //System.IO.StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
                    using (Stream stream = response.GetResponseStream())
                    {
                        try
                        {
                            DownloadPage(keywords, stream, fxUrl1);
                        }
                        catch (Exception ex)
                        {
                            txtLog.BeginInvoke(new Action(() =>
                            {
                                txtLog.AppendText(ex.Message + Environment.NewLine);
                            }));
                        }
                    }
                }
                else
                {
                    MessageBox.Show("获取当前页面数据失败：" + response.StatusCode);
                }
            }
        }

        private void DownloadPage(string keywords, Stream stream, string fxUrl1)
        {
            string[] newurl = new string[2000];
            string strurl = "";
            WebClient myWebClient = new WebClient(); 
            byte[] myDataBuffer = myWebClient.DownloadData(fxUrl1);
            string strWebData = Encoding.Default.GetString(myDataBuffer);
            string charSet = "";
            Match charSetMatch = Regex.Match(strWebData, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            string webCharSet = charSetMatch.Groups[2].Value;
            charSet = webCharSet;
            if (charSet != null && charSet != "")
            {
                using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding(charSet)))
                {
                    string retData = reader.ReadToEnd();
                    //按照正则表达式分析所有的url
                    List<string> lstData = Parser.matchUrl(retData, keywords, zblx);
                    foreach (string strHref in lstData)
                    {
                        //                    Regex RegExFindHref = new Regex(@"<a/s+([^>]*/s*)?href/s*=/s*(?:""(?<1>[//a-z0-9_][^""]*)""|'(?<1>[//a-z0-9_][^']*)'
                        //|(?<1>[//a-z0-9_]/S*))(/s[^>]*)?>(?<2>.*?)</a>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
                        //                    for (Match m = RegExFindHref.Match(strHref); m.Success; m = m.NextMatch())
                        //                    {
                        //                        urlsz += m.Groups[1].ToString();
                        //                    }

                        string[] arrReturn = Parser.ExtractLinks(baseUrl, fxUrl1, strHref, k);
                        foreach (string strTemp in arrReturn)
                        {
                            txtLog.BeginInvoke(new Action(() =>
                            {
                                if (strTemp != "" || !string.IsNullOrEmpty(strTemp))
                                {
                                    strurl += strTemp + ",";
                                }
                                else
                                    strurl += strTemp;
                                strSql = "insert into websearch (id,adress,addtime) values ('" + id + "','" + strTemp + "',sysdate)";
                                SysParams.OAConnection().RunSql(strSql);
                                txtLog.AppendText(strTemp + Environment.NewLine);
                            }));
                        }
                    }
                    //if (strurl.IndexOf(",") > -1)
                    //{
                    //    strurl = strurl.Substring(0, strurl.LastIndexOf(","));
                    //}
                    //strurl=strurl.Substring(0, strurl.Length - 1);
                    newurl = strurl.Split(',');
                    if (strurl != "" || !string.IsNullOrEmpty(strurl))
                    {
                        for (int i = 0; i < newurl.Length - 1; i++)
                        {
                            strSql = "select *from websearch where id='" + id + "' and adress='" + newurl[i] + "'";
                            DataTable dt;
                            SysParams.OAConnection().RunSql(strSql,out dt);
                            if(dt.Rows.Count==0)
                            {
                                k++;
                                ProcessDownload(keywords, newurl[i]);
                            }
                            
                        }
                    }
                    //return strurl;
                    //JObject objRoot = (JObject)JsonConvert.DeserializeObject(json);
                    //JArray imgs = (JArray)objRoot["imgs"];
                    //for (int i = 0; i < imgs.Count; i++)
                    //{
                    //    JObject img = (JObject)imgs[i];
                    //    string objUrl = (string)img["objURL"];//http://hibiadu....../1.jpg
                    //    //txtLog.AppendText(objUrl+Environment.NewLine);
                    //    //保存的路径是：destDir；
                    //    try
                    //    {
                    //        DownloadImage(objUrl);//避免一个方法中的代码过于复杂
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        //子线程的代码中操作界面控件要使用BeginInvoke
                    //        txtLog.BeginInvoke(new Action(() => {
                    //            txtLog.AppendText(ex.Message + Environment.NewLine);
                    //        }));
                    //    }
                    //}
                }
            }
            
        }

        private void DownloadImage(string objURL)
        {
            //UrlRefer：这个图片是从哪个页面启动下载的

            //Path.GetFileName(objURL)得到“1.jpg”
            //Path.Combine是把destDir和1.jpg拼接起来
            string destFile = Path.Combine(destDir, Path.GetFileName(objURL));
            //要保存的文件名
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(objURL);
            request.Referer = "http://image.baidu.com/";//欺骗服务器说“是从百度发出的请求”
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (Stream stream = response.GetResponseStream())
                    using (Stream fsStream = new FileStream(destFile, FileMode.Create))
                    {
                        stream.CopyTo(fsStream);
                    }
                }
                else
                {
                    throw new Exception("下载" + objURL + "失败，错误码：" + response.StatusCode);
                }
            }
        }
    }
}
